package com.goldgov.login.web.model;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.goldgov.authorityInfo.service.AuthorityInfoInit;
import com.goldgov.kcloud.core.cache.CacheHolder;
import com.goldgov.utils.PropertyUtil;

/**
 * 用户对象
 * 
 * @author Tony
 */
public class LoginAuthUser {

	private String entityID;
	private String loginID;
	private String password;
	private String name;// 姓名
	private String userName;// 登录名
	private String nickName;// 昵称
	private String[] userRoles;// 角色数组
	private Object extendAccountInfo;// 用户扩展信息

	public String getLoginID() {
		return loginID;
	}

	public void setLoginID(String loginID) {
		this.loginID = loginID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEntityID(String entityID) {
		this.entityID = entityID;
	}
	
	public String getEntityID() {
		return entityID;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;

	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String[] getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(String[] userRoles) {
		this.userRoles = userRoles;
	}

	public Object getExtendAccountInfo() {
		return extendAccountInfo;
	}

	public void setExtendAccountInfo(Object extendAccountInfo) {
		this.extendAccountInfo = extendAccountInfo;
	}

	/**
	 * 从request中获取前台用户信息
	 * 
	 * @param request
	 * @return
	 */
	public static LoginAuthUser getUser(HttpServletRequest request) {
		String userInfo = (String) request.getSession().getAttribute("USER_JSON");
		if (userInfo != null && !"".equals(userInfo)) {
			try {
				return new ObjectMapper().readValue(userInfo, LoginAuthUser.class);
			} catch (JsonParseException e) {
				e.printStackTrace();
				return null;
			} catch (JsonMappingException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} 
		}
		return null;
	}

	/**
	 * 从request中获取前台用户信息
	 * 
	 * @param request
	 * @return
	 */
	public static LoginAuthUser getUserTest(HttpServletRequest request) {
		
		LoginAuthUser u = new LoginAuthUser();
		u.setEntityID("352b0288-6e5b-11e6-a1f9-507b9dbfbcab");
		u.setUserName("demo1");
		return u;
	}
	
	/**
	 * 从request中获取后台用户信息
	 * @param request
	 * @return
	 */
	public static SwbAuthUser getUserByAdmin(HttpServletRequest request) {
		
//		SwbAuthUser u = new SwbAuthUser("admin","admin","admin");
		SwbAuthUser u = (SwbAuthUser) request.getSession().getAttribute("_$CURRENT_SWB_USER$_");
		return u;
	}
	
	public static SwbAuthUser getUserByAdminTest(HttpServletRequest request) {
		
		SwbAuthUser u = new SwbAuthUser("admin","admin","admin");
		return u;
	}

	/**
	 * 获取前台用户管理范围
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String getPortalUserScope(HttpServletRequest request) {
		// 1:获取当前登录人，从当前登录人信息中获取scopecode
		String EXTEND_JSON = (String) request.getSession().getAttribute("EXTEND_JSON");
		if (EXTEND_JSON == null||"".equals(EXTEND_JSON)) {
			return "";
		}
		try {
			Map map = new ObjectMapper().readValue(EXTEND_JSON, Map.class);
			return map==null?"":map.get("scopeCode").toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取后台管理员管理范围
	 * 
	 * @param request
	 * @return
	 */
	public static String getSwbUserScope(HttpServletRequest request) {
//		String s="-1/";
		String s= (String) request.getSession().getAttribute("_orgScopeSesionId_");
		return s;
	}
	
	public static String getSwbUserScopeTest(HttpServletRequest request) {
		String s="-1/";
		return s;
	}

	@SuppressWarnings("unchecked")
	public static String getScopeCodeByFunctionID(String functionID,HttpServletRequest request) {
		Object o=CacheHolder.get(getUser(request).getEntityID());
		if(PropertyUtil.objectNotEmpty(o)) {
			String auths=o.toString();
			Map<String,String> authsMap=JSONObject.parseObject(auths,Map.class);
			Map<String,String> authorityInfosMap=JSONObject.parseObject(authsMap.get(AuthorityInfoInit.AUTHORITY_INFOS),Map.class);
			String scope=authorityInfosMap.get(functionID);
			if(null==scope) 
				//取默认管理范围
				scope=authsMap.get(AuthorityInfoInit.DEFAULT_SCOPE_CODE);
			return scope;
		}else {
			return getPortalUserScope(request);}
	}
}